/*
 * MODULE  NAME  :
 * PROGRAM NAME  : renrendai.h
 * AUTHOR        : HOTMOCHA
 * CREATE  DATE  : 2015-02-01 20:13:47
 * PROGRAM DESC  :
 *
 * HISTORY       :
 *
 */
#ifndef _H_RENRENDAI_H_
#define _H_RENRENDAI_H_
#include <stdlib.h>
#include <stdio.h>
#include <gumbo.h>
#include <unistd.h>

#define GUMBO_INDEXOVERFLOW				-30001
#define GUMBO_ARGNULL					-30002
#define GUMBO_TAGINVALID				-30003
#define GUMBO_NOTTEXT					-30004
#define GUMBO_NOTELEMENT				-30005
#define GUMBO_NOTFOUND					-30006
#define GUMBO_PARSEERROR				-30007
#define GUMBO_PARAMETER					-30008
#define GUMBO_ATTRERROR					-30009

#define MAXATTRCNT			5
#define MAXATTRIDVALUELEN	101
#define MAXHTMLDEPATH		30
#define	UNDEFINED			-1	
#define __HTMLPATHNODEEND__	{UNDEFINED,UNDEFINED,UNDEFINED,{""},{""}}
/* TODO: 
   1. 在HtmlPathNode 中添加回调函数，当满足就会调用，原先 int (EnterNode*)(void *);
   2. 将每次的解析做成一个env环境(里面可以有一个debug的开关，root，深度，用于保存解析出来变量的结构体等
   */
typedef struct {
	int tag;
	int index; 
	/* 如果tag!=UNDEFINED,第index个并且满足tag添加，
	   如果tag==UNDEFINED,指父节点下的第index 节点,下标从1开始 */
	int type;
	char attrname[MAXATTRCNT][MAXATTRIDVALUELEN];
	char attrvalue[MAXATTRCNT][MAXATTRIDVALUELEN];
} HtmlPathNode;
extern const HtmlPathNode ENDNODE;

int GetTag(GumboNode *n);
int GetLen(GumboNode *n);
int FindChildByIndex(GumboNode *n, unsigned int index, GumboNode **child);
int FindChildByTag(GumboNode *n, int tag, GumboNode **child);
int FindChildByTagAndAttr(GumboNode *n, int tag, char *id, char *value, GumboNode **child);
int GetNodeType(GumboNode *n);
GumboAttribute* GetAttr(GumboNode *n, char *id);
int GetAttrByID(GumboNode *n, char *id, const char **value);
int WalkHtmlPath(GumboNode *root, HtmlPathNode *path, GumboNode **node);
int CheckAttr(GumboNode *root, HtmlPathNode *path);
const char *TypeEnumToName(GumboNodeType d);
const char *TagEnumToName(GumboTag d);
int HtmlGetAttrValueByPath(GumboNode *root, HtmlPathNode *path, char *id, char *text, int len);
int HtmlGetTextByPath(GumboNode *root, HtmlPathNode *path, char *text, int len);
void GetAllAttrByTag(GumboNode *node, GumboTag tag, char* id, void (*func)(const char*));

#endif
